package com.kkwrite.regimen.provider.service.param;

import com.kkwrite.regimen.common.entity.dto.param.QueryParamOutDTO;
import com.kkwrite.regimen.provider.domain.RegParamDO;
import com.kkwrite.regimen.provider.repository.RegParamRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lisha on 2018/7/22 11:49.
 *
 * @author lisha
 */
@Service("paramService")
public class ParamServiceImpl implements ParamService {

    @Resource(name = "regParamRepository")
    private RegParamRepository repository;

    @Override
    public QueryParamOutDTO queryParam(Short type, String name) {
        RegParamDO exampleDO = new RegParamDO();
        exampleDO.setType(type);
        exampleDO.setKey(name);
        List<RegParamDO> queriedList = repository.findAll(Example.of(exampleDO));
        final QueryParamOutDTO out = new QueryParamOutDTO();
        final List<QueryParamOutDTO.Data> outList = out.getParamData();
        queriedList.forEach((paramDO) -> {
            QueryParamOutDTO.Data data = out.createData();
            BeanUtils.copyProperties(paramDO, data);
            outList.add(data);
        });
        return out;
    }

}
